package 刷题.leedcode31;

import javax.swing.plaf.nimbus.State;
import java.util.Stack;

//栈的压入，弹出序列
public class Solution1 {
    public boolean IsPopOrder(int[] pushA,int[] popA){
        int n = pushA.length;
        //创建栈
        Stack<Integer> s = new Stack<>();
        int j = 0;
        for (int i = 0; i < n; i++) {
            //当j<n并且栈为空或者栈顶不等于出栈数组
            while(j < n && (s.isEmpty() || s.peek() != popA[i])){
                s.push(pushA[j]);//添加
                j++;
            }
            //栈顶出现数组元素
            if(s.peek( )== popA[i]){//返回并比较
                s.pop();//移除
            }else{
                return false;
            }
        }
        return s.isEmpty();
    }
}
